package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Applications;
import com.example.springboot.service.ApplicationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/applications")
public class ApplicationsController {

    @Autowired
    private ApplicationsService applicationsService;

    @GetMapping
    public Result getAllApplications() {
        try {
            List<Applications> applications = applicationsService.findAllApplications();
            return Result.success(applications);
        } catch (Exception e) {
            return Result.error("500", "获取申请列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result getApplicationById(@PathVariable int id) {
        try {
            Applications application = applicationsService.findApplicationById(id);
            return Result.success(application);
        } catch (Exception e) {
            return Result.error("500", "获取申请详情失败");
        }
    }

    @GetMapping("/student/{student_id}")
    public Result getApplicationsByStudentId(@PathVariable String student_id) {
        try {
            List<Applications> applications = applicationsService.findapplicationsByStudentId(student_id);
            return Result.success(applications);
        } catch (Exception e) {
            return Result.error("500", "获取学生申请列表失败");
        }
    }

    @PostMapping
    public Result createApplication(@RequestBody Applications application) {
        try {
            applicationsService.saveApplication(application);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "创建申请失败");
        }
    }

    @PutMapping("/{id}")
    public Result updateApplication(@PathVariable int id, @RequestBody Applications application) {
        try {
            application.setApplication_id(id);
            applicationsService.saveApplication(application);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "更新申请失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteApplication(@PathVariable int id) {
        try {
            applicationsService.deleteApplication(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "删除申请失败");
        }
    }

    @PostMapping("/{id}/register")
    public Result registerForTest(@PathVariable int id) {
        try {
            applicationsService.registerForTest(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    @GetMapping("/getAllApplications")
    public Result getAllApplications2() {
        try {
            List<Applications> applications =applicationsService.getAllApplications();
            return Result.success(applications);
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    @PutMapping("/new")
    public Result updateApplicationN(@RequestBody Applications application) {
        try {
            applicationsService.saveApplicationN(application.getApplication_id(),application.getApplication_status());
            return Result.success();
        } catch (Exception e) {
            return Result.error("500", "更新申请失败");
        }
    }


    @PostMapping("/apply")
    public Result applyInsert(@RequestBody Applications application) {
        applicationsService.applyInsert(application);
        return Result.success();
    }
}